import irrlicht;
import std.stdio;

struct SAppContext
{
    IrrlichtDevice device;
    int counter = 0;
    IGUIListBox listbox;
}

enum
{
    GUI_ID_QUIT_BUTTON = 101,
    GUI_ID_NEW_WINDOW_BUTTON,
    GUI_ID_FILE_OPEN_BUTTON,
    GUI_ID_TRANSPARENCY_SCROLL_BAR
}

class MyEventReceiver : IEventReceiver
{
    this(SAppContext context) { Context = context; }

    override bool OnEvent(SEvent event)
    {
        if(event.EventType == EEVENT_TYPE.EET_GUI_EVENT)
        {
            int id = event.GUIEvent.Caller.getID();

            IGUIEnvironment env = Context.device.getGUIEnvironment();

            switch(event.GUIEvent.EventType)
            {
            case EGUI_EVENT_TYPE.EGET_SCROLL_BAR_CHANGED:
                if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
                {
                    assert(event.GUIEvent.Caller);
                    int pos = (new IGUIScrollBar(IGUIElement.swigGetCPtr(event.GUIEvent.Caller), false)).getPos();
                    for(uint i=0; i<EGUI_DEFAULT_COLOR.EGDC_COUNT; ++i)
                    {
                        SColor col = env.getSkin().getColor(cast(EGUI_DEFAULT_COLOR)i);
                        col.setAlpha(pos);
                        env.getSkin().setColor(cast(EGUI_DEFAULT_COLOR)i, col);
                    }
                }
                break;
                
            case EGUI_EVENT_TYPE.EGET_BUTTON_CLICKED:
                switch(id)
                {
                case GUI_ID_QUIT_BUTTON:
                    Context.device.closeDevice();
                    return true;
                case GUI_ID_NEW_WINDOW_BUTTON:
                    {
                    Context.listbox.addItem("Window created");
                    Context.counter += 30;
                    if(Context.counter > 200)
                        Context.counter = 0;
                    IGUIWindow window = env.addWindow(
                                            new IntRect(100 + Context.counter,
                                                        100 + Context.counter,
                                                        300 + Context.counter,
                                                        200 + Context.counter),
                                            false, //modal?
                                            "Test window");
                    env.addStaticText("Please close me",
                                      new IntRect(35,35,140,50),
                                      true,
                                      false,
                                      window);
                    }
                    return true;
                case GUI_ID_FILE_OPEN_BUTTON:
                    Context.listbox.addItem("File open");
                    env.addFileOpenDialog("Please choose a file.");
                    return true;
                default:
                    return false;
                }
            default:
                break;
            }
        }
        return false;
    }
    
    private SAppContext Context;
}


void main()
{
    E_DRIVER_TYPE driverType;
	writeln("Please select the driver you want for this example:\n"~
			" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"~
			" (d) Software Renderer\n (e) Burning's Software Renderer\n"~
			" (f) NullDevice\n (otherKey) exit\n\n");
			
	switch(readln()[0])
	{
		case 'a': driverType = E_DRIVER_TYPE.EDT_DIRECT3D9;break;
		case 'b': driverType = E_DRIVER_TYPE.EDT_DIRECT3D8;break;
		case 'c': driverType = E_DRIVER_TYPE.EDT_OPENGL;break;
		case 'd': driverType = E_DRIVER_TYPE.EDT_SOFTWARE;break;
		case 'e': driverType = E_DRIVER_TYPE.EDT_BURNINGSVIDEO;break;
		case 'f': driverType = E_DRIVER_TYPE.EDT_NULL;break;
		default: assert(0, "No driver selected.");
	}

	IrrlichtDevice device = createDevice(driverType, new UintDimension2D(640, 480), 16, false, false, false, null);
	assert(device, "Unable to create device.");
    
    device.setWindowCaption("Irrlicht with D via SWIG - User Interface Demo");
    device.setResizable(true);
    
    IVideoDriver driver = device.getVideoDriver();
    IGUIEnvironment env = device.getGUIEnvironment();
    
    IGUISkin skin = env.getSkin();
    IGUIFont font = env.getFont("media/fonthaettenschweiler.bmp");
    if(font)
        skin.setFont(font);
    skin.setFont(env.getBuiltInFont(), EGUI_DEFAULT_FONT.EGDF_TOOLTIP);
    
    env.addButton(new IntRect(10,240,110,240+32), null, GUI_ID_QUIT_BUTTON, "Quit", "Exits Program");
    env.addButton(new IntRect(10,280,110,280+32), null, GUI_ID_NEW_WINDOW_BUTTON, "New Window", "Launch a new Window");
    env.addButton(new IntRect(10,320,110,320+32), null, GUI_ID_FILE_OPEN_BUTTON, "File Open", "Opens a file");
    
    env.addStaticText("Transparent Control: ", new IntRect(150,20,350,40), true);
    IGUIScrollBar scrollbar = env.addScrollBar(true, new IntRect(150,45,350,60), null, GUI_ID_TRANSPARENCY_SCROLL_BAR);
    scrollbar.setMax(255);
    
    scrollbar.setPos(env.getSkin().getColor(EGUI_DEFAULT_COLOR.EGDC_WINDOW).getAlpha());
    
    env.addStaticText("Logging ListBox:", new IntRect(50,100,250,130), true);
    IGUIListBox listbox = env.addListBox(new IntRect(50,140,250,210));
    env.addEditBox("Editable Text", new IntRect(350,80,550,100));
    
    SAppContext context = SAppContext();
    context.device = device;
    context.counter = 0;
    context.listbox = listbox;
    
    auto receiver = new MyEventReceiver(context);
    
    device.setEventReceiver(receiver);
    
    env.addImage(driver.getTexture("media/irrlichtlogo2.png"), new IntVector2D(10,10));
    
    while(device.run() && driver)
    if(device.isWindowActive())
    {
        driver.beginScene(true, true, new SColor(0,200,200,200));
        
        env.drawAll();
        
        driver.endScene();
    }
    return;
}